package cn.seaboot.admin.reverse.web;

import cn.seaboot.admin.mvc.Pagination;
import cn.seaboot.admin.mvc.SystemConstant;
import cn.seaboot.admin.mvc.argument.Params;
import cn.seaboot.flake.entity.MappingColumn;
import cn.seaboot.flake.entity.MappingTable;
import cn.seaboot.admin.reverse.service.MappingService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;


/**
 * 逆向工程配置
 *
 * @author postm
 */
@Controller
@Tag(name = "逆向工程配置")
@RequestMapping("db/mapping")
public class MappingCtrl {

    @Resource
    private MappingService mappingService;

    /**
     * 获取映射信息
     *
     * @param tableName 表名
     * @return 映射信息
     */
    @ResponseBody
    @Operation(description = "查询映射配置")
    @Parameters({
            @Parameter(name = "schemaName", description = "数据库名"),
            @Parameter(name = "tableName", description = "表名")
    })
    @RequestMapping(value = "table", method = RequestMethod.GET)
    public MappingTable queryByType(@Params String schemaName, @Params String tableName) {
        return mappingService.queryMappingTable(schemaName, tableName);
    }

    /**
     * 修改映射配置
     *
     * @param table pojo
     * @return 操作结果
     */
    @ResponseBody
    @Operation(description = "保存表映射配置")
    @RequestMapping(value = "table", method = RequestMethod.PUT)
    public int saveDbMappingTable(@Validated MappingTable table) {
        return mappingService.updateMappingTable(table);
    }

    /**
     * 修改映射配置
     *
     * @param column pojo
     * @return 操作结果
     */
    @ResponseBody
    @Operation(description = "保存列映射配置")
    @RequestMapping(value = "column", method = RequestMethod.PUT)
    public int saveConfigure(@Validated MappingColumn column) {
        return mappingService.updateMappingColumn(column);
    }

    /**
     * 编辑页面
     *
     * @param schemaName 数据库名称
     */
    @ResponseBody
    @Operation(description = "查询表映射信息")
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public PageInfo<MappingTable> schemaName(
            @RequestParam Map<String, Object> params,
            @RequestParam(defaultValue = SystemConstant.DATABASE_ID) String schemaName) {
        params.put("schemaName", schemaName);
        return Pagination.get(params, () -> mappingService.queryMappingTables(params));
    }
}
